package Controllers;

import Configs.Config;
import com.db4o.ObjectSet;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import javax.swing.JOptionPane;
import models.ChiTietGia;
import models.ChuNhaTro;
import models.DiaChi;
import models.HoaDon;
import models.HopDong;
import models.NguoiThue;
import models.NhaTro;
import models.NhanVien;
import models.PhongTro;

public class GeneratetPDF {

    private final Config conn;

    public GeneratetPDF(Config conn) {
        this.conn = conn;
    }

    private String getCurrentPath() {
        File currentDirFile = new File(".");
        String currentPathProject = currentDirFile.getAbsolutePath();
        return currentPathProject.substring(0, currentPathProject.length() - 1);
    }

    //thong ke hop dong cua tung nhan vien
    public String generatethongke(String msnv) throws ParseException {
        NhanVienController_2 nhanVienControl = new NhanVienController_2(this.conn);
        NhanVien nhanVienXem = nhanVienControl.getWithMSNV(msnv);
        ObjectSet<NhanVien> dsNhanVien = nhanVienControl.getAllNhanVienNotIsdelete();

        String urlFileCreate = this.getCurrentPath() + "src/files/" + nhanVienXem.getMSNV() + ".html";
        try {
            BufferedWriter writer;
            try (BufferedReader in = new BufferedReader(new FileReader(this.getCurrentPath() + "src/files/temp_thongkehopdong.html"))) {
                writer = new BufferedWriter(new FileWriter(urlFileCreate));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    try {
                        switch (inputLine.trim()) {
                            case "FLAG1":
                                //Thông tin nhan vien va hop dong
                                int stt = 0;
                                inputLine = "";
                                for (NhanVien nhanvien : dsNhanVien) {
                                    Set<HopDong> dsHopDong = nhanvien.getDshopdong();
                                            inputLine += "<h3 style=\"text-align:left\">"+nhanvien.getHoten()+" MSNV "+ nhanvien.getMSNV()+"."+"</h3>";                                          
                                            inputLine += "  <table align=\"center\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n"
                                                    + "            <tbody>\n"
                                                    + "                <tr>\n"
                                                    + "                    <td style=\"width:10%\">\n"
                                                    + "                        <p style=\"text-align:center\"><center><strong>STT</strong></center></p>\n"
                                                    + "                    </td>\n"
                                                    + "                    <td style=\"width:40%\">\n"
                                                    + "                        <p style=\"text-align:center\"><strong>Mã Hợp Đồng</strong></p>\n"
                                                    + "                    </td>\n"
                                                    + "                    <td style=\"width:30%\">\n"
                                                    + "                        <p style=\"text-align:center\"><strong>Ngày Lập</strong></p>\n"
                                                    + "                    </td>\n"
                                                    + "                    <td style=\"width:20%\">\n"
                                                    + "                        <p style=\"text-align:center\"><strong>Người Thuê</strong></p>\n"
                                                    + "                    </td>\n"
                                                    + "                </tr>";
                                    
                                    for (HopDong hopdong : dsHopDong) {
                                        stt++;
                                        inputLine += " <tr>\n"
                                                + "                    <td style=\"width:10%\">\n"
                                                + "                        <p>" + stt + "</p>\n"
                                                + "                    </td>\n"
                                                + "                    <td style=\"width:40%\">\n"
                                                + "                        <p>" + hopdong.getMSHD() + "</p>\n"
                                                + "                    </td>\n"
                                                + "                    <td style=\"width:30%\">\n"
                                                + "                        <p>" + hopdong.getThoidiemlap() + "</p>\n"
                                                + "                    </td>\n"
                                                + "                    <td style=\"width:20%\">\n"
                                                + "                        <p>" + hopdong.getNguoithue().getHoten() + "-SDT :"+ hopdong.getNguoithue().getSdt() + "</p>\n"
                                                + "                    </td>\n"
                                                + "                </tr>";

                                    }
                                 
                                
                                inputLine += "\n"
                                        + "            </tbody>\n"
                                             + "        </table>";
                                inputLine +="<h3 style=\"text-align:right\">"+"Tổng số hợp đồng được lập : "+stt+"."+"</h3>";
                                stt = 0;
                                }
                                break;
                            case "FLAG2":
                                //Ngày in người in
                                Calendar cal = Calendar.getInstance();
                                SimpleDateFormat sdfwithtime = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
                                inputLine = "<i>Thời điểm in:&nbsp;" + sdfwithtime.format(cal.getTime()) + ".<br/>Người in:&nbsp;(" + nhanVienXem.getMSNV() + ") " + nhanVienXem.getHoten() + ".</i>";
                                break;
                        }
                        writer.write(inputLine + "\n");
                    } catch (IOException e) {
                    }
                }
            }

            writer.close();
        } catch (Exception e) {
        }
        String urlFilePDF = this.getCurrentPath() + "src/files/" + nhanVienXem.getMSNV() + ".pdf";
        if (this.convertToPDF(urlFileCreate, urlFilePDF)) {
            try {
                File file = new File(urlFileCreate);
                if (file.delete()) {
                    System.out.println(file.getName() + " is deleted!");
                } else {
                    System.out.println("Delete operation is failed.");
                }
                return urlFilePDF;

            } catch (Exception e) {
            }
        } else {
            try {
                File file = new File(urlFileCreate);
                if (file.delete()) {
                    System.out.println(file.getName() + " is deleted!");
                } else {
                    System.out.println("Delete operation is failed.");
                }
            } catch (Exception e) {
            }
        }
        return null;
    }

    public String generateChiTietNhaTro(String msnv, String soGPDKKD) throws ParseException {
        NhanVienController_2 nhanVienControl = new NhanVienController_2(this.conn);
        NhanVien nhanVienXem = nhanVienControl.getWithMSNV(msnv);

        NhaTroController nhaTroControl = new NhaTroController(this.conn);
        NhaTro nhaTroXem = nhaTroControl.getNhaTroWithGPKD(soGPDKKD);
        ChuNhaTro chuNhaTroXem = nhaTroXem.getChunhatro();

        GiaPhongController giaPhongControl = new GiaPhongController(this.conn);

        String urlFileCreate = this.getCurrentPath() + "src/files/" + nhaTroXem.getGPKD() + ".html";
        try {
            BufferedWriter writer;
            try (BufferedReader in = new BufferedReader(new FileReader(this.getCurrentPath() + "src/files/temp_chitietnhatro.html"))) {
                writer = new BufferedWriter(new FileWriter(urlFileCreate));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    try {
                        switch (inputLine.trim()) {
                            case "FLAG1":
                                //Thông tin chủ nhà trọ
                                DiaChi dc = chuNhaTroXem.getDiachi();
                                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                                inputLine = " <li>&nbsp;Số CMND: " + chuNhaTroXem.getCMND() + "</li>\n";
                                inputLine += " <li>&nbsp;Tên chủ nhà trọ: " + chuNhaTroXem.getHoten() + "</li>\n";
                                inputLine += " <li>Giới t&iacute;nh:&nbsp;" + chuNhaTroXem.getGioitinh() + "</li>\n";
                                inputLine += " <li>Ng&agrave;y sinh:&nbsp;" + sdf.format(chuNhaTroXem.getNgaysinh()) + "</li>\n";
                                inputLine += " <li>SĐT:&nbsp;" + chuNhaTroXem.getSdt() + "</li>\n";
                                inputLine += " <li>Địa chỉ:&nbsp;" + dc.getSonha() + "/" + dc.getHem() + ", " + dc.getDuong() + ", " + dc.getQuanhuyen() + ", " + dc.getTinhtp() + "</li>\n";
                                break;
                            case "FLAG2":
                                //Thông tin nhà trọ
                                dc = nhaTroXem.getDiachi();
                                inputLine = "<li>Số giấy ph&eacute;p đăng k&yacute; kinh doanh:&nbsp;" + nhaTroXem.getGPKD() + "</li>";
                                inputLine += "<li>T&ecirc;n nh&agrave; trọ:&nbsp;" + nhaTroXem.getTennhatro() + "</li>";
                                inputLine += "<li>M&ocirc; tả nh&agrave; trọ:&nbsp;" + nhaTroXem.getMotanhatro() + "</li>";
                                inputLine += "<li>SĐT:&nbsp;" + nhaTroXem.getSdt() + "</li>";
                                inputLine += "<li>Địa chỉ:&nbsp;" + dc.getSonha() + "/" + dc.getHem() + ", " + dc.getDuong() + ", " + dc.getQuanhuyen() + ", " + dc.getTinhtp() + "</li>";
                                break;
                            case "FLAG3":
                                //Thông tin ds phòng trọ
                                int stt = 0;
                                inputLine = "";
                                Set<PhongTro> dsPhongTro = nhaTroXem.getDsphongtro();
                                for (PhongTro phongTro : dsPhongTro) {
                                    stt++;
                                    inputLine += " <tr>\n"
                                            + "                    <td style=\"width:10%\">\n"
                                            + "                        <p>" + stt + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:40%\">\n"
                                            + "                        <p>" + phongTro.getMaphong() + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:30%\">\n"
                                            + "                        <p>" + XuLyCurrency.priceWithDecimal(giaPhongControl.giaPhong(phongTro.getLoaiphong().getDschitietgia())) + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:20%\">\n"
                                            + "                        <p>" + (phongTro.getTinhtrang() ? "Còn trống" : "Đang cho thuê") + "</p>\n"
                                            + "                    </td>\n"
                                            + "                </tr>";
                                }
                                break;
                            case "FLAG4":
                                //Ngày in người in
                                Calendar cal = Calendar.getInstance();
                                SimpleDateFormat sdfwithtime = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
                                inputLine = "<i>Thời điểm in:&nbsp;" + sdfwithtime.format(cal.getTime()) + ".<br/>Người in:&nbsp;(" + nhanVienXem.getMSNV() + ") " + nhanVienXem.getHoten() + ".</i>";
                                break;
                        }
                        writer.write(inputLine + "\n");
                    } catch (Exception e) {
                    }
                }
            }

            writer.close();
        } catch (IOException e) {
        }
        String urlFilePDF = this.getCurrentPath() + "src/files/" + nhaTroXem.getGPKD() + ".pdf";
        if (this.convertToPDF(urlFileCreate, urlFilePDF)) {
            try {
                File file = new File(urlFileCreate);
                if (file.delete()) {
                    System.out.println(file.getName() + " is deleted!");
                } else {
                    System.out.println("Delete operation is failed.");
                }
                return urlFilePDF;

            } catch (Exception e) {
            }
        } else {
            try {
                File file = new File(urlFileCreate);
                if (file.delete()) {
                    System.out.println(file.getName() + " is deleted!");
                } else {
                    System.out.println("Delete operation is failed.");
                }
            } catch (Exception e) {
            }
        }
        return null;
    }

    public String generateChiTietPhongTro(String msnv, String maPhong) {
        NhanVienController_2 nhanVienControl = new NhanVienController_2(this.conn);
        NhanVien nhanVienXem = nhanVienControl.getWithMSNV(msnv);

        PhongTroController phongTroControl = new PhongTroController(this.conn);
        PhongTro phongTroXem = phongTroControl.getNhaTroWithMaPhong(maPhong);
        NhaTro nhaTroXem = phongTroXem.getNhatro();
        ChuNhaTro chuNhaTroXem = nhaTroXem.getChunhatro();
        Set<ChiTietGia> dsChiTietGia = phongTroXem.getLoaiphong().getDschitietgia();
        Set<HopDong> dsHopDong = phongTroXem.getDshopdong();

        String urlFileCreate = this.getCurrentPath() + "src/files/" + phongTroXem.getMaphong() + ".html";
        try {
            BufferedWriter writer;
            try (BufferedReader in = new BufferedReader(new FileReader(this.getCurrentPath() + "src/files/temp_chitietphongtro.html"))) {
                writer = new BufferedWriter(new FileWriter(urlFileCreate));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    try {
                        switch (inputLine.trim()) {
                            case "FLAG1":
                                //Thông tin chủ nhà trọ
                                DiaChi dc = chuNhaTroXem.getDiachi();
                                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                                inputLine = " <li>&nbsp;Số CMND: " + chuNhaTroXem.getCMND() + "</li>\n";
                                inputLine += " <li>&nbsp;Tên chủ nhà trọ: " + chuNhaTroXem.getHoten() + "</li>\n";
                                inputLine += " <li>Giới t&iacute;nh:&nbsp;" + chuNhaTroXem.getGioitinh() + "</li>\n";
                                inputLine += " <li>Ng&agrave;y sinh:&nbsp;" + sdf.format(chuNhaTroXem.getNgaysinh()) + "</li>\n";
                                inputLine += " <li>SĐT:&nbsp;" + chuNhaTroXem.getSdt() + "</li>\n";
                                inputLine += " <li>Địa chỉ:&nbsp;" + dc.getSonha() + "/" + dc.getHem() + ", " + dc.getDuong() + ", " + dc.getQuanhuyen() + ", " + dc.getTinhtp() + "</li>\n";
                                break;
                            case "FLAG2":
                                //Thông tin nhà trọ
                                dc = nhaTroXem.getDiachi();
                                inputLine = "<li>Số giấy ph&eacute;p đăng k&yacute; kinh doanh:&nbsp;" + nhaTroXem.getGPKD() + "</li>";
                                inputLine += "<li>T&ecirc;n nh&agrave; trọ:&nbsp;" + nhaTroXem.getTennhatro() + "</li>";
                                inputLine += "<li>M&ocirc; tả nh&agrave; trọ:&nbsp;" + nhaTroXem.getMotanhatro() + "</li>";
                                inputLine += "<li>SĐT:&nbsp;" + nhaTroXem.getSdt() + "</li>";
                                inputLine += "<li>Địa chỉ:&nbsp;" + dc.getSonha() + "/" + dc.getHem() + ", " + dc.getDuong() + ", " + dc.getQuanhuyen() + ", " + dc.getTinhtp() + "</li>";
                                break;
                            case "FLAG3":
                                //Thông tin ds phòng trọ
                                inputLine = "<li>M&atilde; ph&ograve;ng:&nbsp;" + phongTroXem.getMaphong() + "</li>\n"
                                        + "<li>T&igrave;nh trạng hiện tại:&nbsp;" + (phongTroXem.getTinhtrang() ? "Còn trống" : "Đang cho thuê") + "</li>";
                                break;
                            case "FLAG4":
                                //Thông tin lịch sử giá
                                int stt = 0;
                                inputLine = "";
                                sdf = new SimpleDateFormat("dd-MM-yyyy");
                                for (ChiTietGia chiTietGia : dsChiTietGia) {
                                    stt++;
                                    inputLine += "   <tr>\n"
                                            + "                    <td style=\"width:20%\">\n"
                                            + "                        <p>" + stt + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:40%\">\n"
                                            + "                        <p>" + XuLyCurrency.priceWithDecimal(chiTietGia.getGiaphong().getGia() + "") + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:40%\">\n"
                                            + "                        <p>" + sdf.format(chiTietGia.getThoidiemapdung()) + "</p>\n"
                                            + "                    </td>\n"
                                            + "                </tr>";
                                }
                                break;
                            case "FLAG5":
                                //Thông tin ds hợp đồng
                                stt = 0;
                                inputLine = "";
                                sdf = new SimpleDateFormat("dd-MM-yyyy");
                                for (HopDong hopDong : dsHopDong) {
                                    stt++;
                                    inputLine = "<tr>\n"
                                            + "                    <td style=\"width:10%\">\n"
                                            + "                        <p>" + stt + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:25%\">\n"
                                            + "                        <p>" + hopDong.getMSHD() + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:25%\">\n"
                                            + "                        <p>" + sdf.format(hopDong.getThoidiemlap()) + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:20%\">\n"
                                            + "                        <p>" + sdf.format(hopDong.getNgaybatdau()) + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:20%\">\n"
                                            + "                        <p>" + hopDong.getGhichu() + "</p>\n"
                                            + "                    </td>\n"
                                            + "                </tr>";
                                }
                                break;
                            case "FLAG6":
                                //Ngày in người in
                                Calendar cal = Calendar.getInstance();
                                SimpleDateFormat sdfwithtime = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
                                inputLine = "<i>Thời điểm in:&nbsp;" + sdfwithtime.format(cal.getTime()) + ".<br/>Người in:&nbsp;(" + nhanVienXem.getMSNV() + ") " + nhanVienXem.getHoten() + ".</i>";
                                break;
                        }
                        writer.write(inputLine + "\n");
                    } catch (IOException e) {
                    }
                }
            }

            writer.close();
        } catch (IOException e) {
        }
        String urlFilePDF = this.getCurrentPath() + "src/files/" + phongTroXem.getMaphong() + ".pdf";
        if (this.convertToPDF(urlFileCreate, urlFilePDF)) {
            try {
                File file = new File(urlFileCreate);
                if (file.delete()) {
                    System.out.println(file.getName() + " is deleted!");
                } else {
                    System.out.println("Delete operation is failed.");
                }
                return urlFilePDF;

            } catch (Exception e) {
            }
        } else {
            try {
                File file = new File(urlFileCreate);
                if (file.delete()) {
                    System.out.println(file.getName() + " is deleted!");
                } else {
                    System.out.println("Delete operation is failed.");
                }
            } catch (Exception e) {
            }
        }
        return null;
    }

    public String generateChiTietHopDong(String msnv, String maHopDong) {
        NhanVienController_2 nhanVienControl = new NhanVienController_2(this.conn);
        NhanVien nhanVienIn = nhanVienControl.getWithMSNV(msnv);
        HopDongController hopDongControl = new HopDongController(this.conn);
        HopDong hopDongXem = hopDongControl.getWithMaHopDong(maHopDong);

        NhanVien nhanVienLapHopDong = hopDongXem.getNhanvien();
        PhongTro phongTroXem = hopDongXem.getPhongtro();
        NhaTro nhaTroXem = phongTroXem.getNhatro();
        ChuNhaTro chuNhaTroXem = nhaTroXem.getChunhatro();
        NguoiThue nguoiThue = hopDongXem.getNguoithue();
        String urlFileCreate = this.getCurrentPath() + "src/files/" + hopDongXem.getMSHD()+ ".html";
        try {
            BufferedWriter writer;
            try (BufferedReader in = new BufferedReader(new FileReader(this.getCurrentPath() + "src/files/temp_chitiethopdong.html"))) {
                writer = new BufferedWriter(new FileWriter(urlFileCreate));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    try {
                        switch (inputLine.trim()) {
                            case "FLAG1":
                                //Thông tin bên cho thuê
                                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                                inputLine = " <li>&nbsp;Số CMND: " + chuNhaTroXem.getCMND() + "</li>\n";
                                inputLine += " <li>&nbsp;Tên chủ nhà trọ: " + chuNhaTroXem.getHoten() + "</li>\n";
                                inputLine += " <li>Giới t&iacute;nh:&nbsp;" + chuNhaTroXem.getGioitinh() + "</li>\n";
                                inputLine += " <li>SĐT:&nbsp;" + chuNhaTroXem.getSdt() + "</li>\n";
                                break;
                            case "FLAG2":
                                //Thông tin bên thuê
                                DiaChi dc = nhaTroXem.getDiachi();
                                inputLine = " <li>&nbsp;Số CMND: " + nguoiThue.getCMND() + "</li>\n";
                                inputLine += " <li>&nbsp;Tên chủ nhà trọ: " + nguoiThue.getHoten() + "</li>\n";
                                inputLine += " <li>Giới t&iacute;nh:&nbsp;" + nguoiThue.getGioitinh() + "</li>\n";
                                inputLine += " <li>Công việc:&nbsp;" + nguoiThue.getCongviec()+ "</li>\n";
                                break;
                             case "FLAG3":
                                //Thông tin nhà trọ thuê
                                dc = nhaTroXem.getDiachi();
                                inputLine = "<li>Tên nhà trọ:&nbsp;" + nhaTroXem.getTennhatro() + "</li>";
                                inputLine += "<li>Địa chỉ:&nbsp;" + dc.getSonha() + "/" + dc.getHem() + ", " + dc.getDuong() + ", " + dc.getQuanhuyen() + ", " + dc.getTinhtp() + "</li>";
                                inputLine += "<li>Mã phòng:&nbsp;" + phongTroXem.getMaphong() + "</li>";
                                inputLine += "<li>Số người ở:&nbsp;" + phongTroXem.getLoaiphong().getSucchua() + "</li>";
                                inputLine += "<li>Loại phòng:&nbsp;" + phongTroXem.getLoaiphong().getMotaloai() + "</li>";
                                 break;
                            case "FLAG4":
                                //Thông tin danh sách các tháng đóng
                                int stt = 0;
                                inputLine = "";
                                SimpleDateFormat sdfwithtime = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
                                List<HoaDon> dsHoaDon = hopDongXem.getDshoadon();
                                for (HoaDon hoaDon : dsHoaDon) {
                                    stt++;
                                    inputLine += "   <tr>\n"
                                            + "                    <td style=\"width:10%\">\n"
                                            + "                        <p>" + stt + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:40%\">\n"
                                            + "                        <p>" + hoaDon.getMahoadon() + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:30%\">\n"
                                            + "                        <p>" + sdfwithtime.format(hoaDon.getThoidiemdong()) + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:20%\">\n"
                                            + "                        <p>" + hoaDon.getGhichu()+ "</p>\n"
                                            + "                    </td>\n"
                                            + "                </tr>";
                                }
                                break;
                            case "FLAG5":
                                //Ngày in người in - nguoi lập - thoi gian lập
                                sdfwithtime = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
                                Calendar cal = Calendar.getInstance();
                                inputLine = "<i>Thời điểm lập hợp đồng:&nbsp;" + sdfwithtime.format(hopDongXem.getThoidiemlap()) + ".<br/>Nhân viên lập:&nbsp;(" + nhanVienLapHopDong.getMSNV() + ") " + nhanVienLapHopDong.getHoten() + ".</i><br/><i>Thời điểm in:&nbsp;" + sdfwithtime.format(cal.getTime()) + ".<br/>Nhân viên in:&nbsp;(" + nhanVienIn.getMSNV() + ") " + nhanVienIn.getHoten() + ".</i>";
                                break;
                        }
                        writer.write(inputLine + "\n");
                    } catch (IOException e) {
                    }
                }
            }

            writer.close();
        } catch (IOException e) {
        }
        String urlFilePDF = this.getCurrentPath() + "src/files/" + hopDongXem.getMSHD()+ ".pdf";
        if (this.convertToPDF(urlFileCreate, urlFilePDF)) {
            try {
                File file = new File(urlFileCreate);
                if (file.delete()) {
                    System.out.println(file.getName() + " is deleted!");
                } else {
                    System.out.println("Delete operation is failed.");
                }
                return urlFilePDF;

            } catch (Exception e) {
            }
        } else {
            try {
                File file = new File(urlFileCreate);
                if (file.delete()) {
                    System.out.println(file.getName() + " is deleted!");
                } else {
                    System.out.println("Delete operation is failed.");
                }
            } catch (Exception e) {
            }
        }
        return null;
    }

    public String generateChiTietHoaDon(String msnv, String maHoaDon) throws ParseException {
        NhanVienController_2 nhanVienControl = new NhanVienController_2(this.conn);
        NhanVien nhanVienIn = nhanVienControl.getWithMSNV(msnv);

        HoaDonController hoaDonControl = new HoaDonController(this.conn);
        HoaDon hoaDonXem = hoaDonControl.getWithMaHoaDon(maHoaDon);

        PhongTro phongTroXem = hoaDonXem.getHopdong().getPhongtro();
        NhaTro nhaTroXem = phongTroXem.getNhatro();
        NhanVien nhanVienLapHoaDon = hoaDonXem.getNhanvien();
        NguoiThue nguoiThue = hoaDonXem.getHopdong().getNguoithue();

        String urlFileCreate = this.getCurrentPath() + "src/files/" + hoaDonXem.getMahoadon() + ".html";
        try {
            BufferedWriter writer;
            try (BufferedReader in = new BufferedReader(new FileReader(this.getCurrentPath() + "src/files/temp_chitiethoadon.html"))) {
                writer = new BufferedWriter(new FileWriter(urlFileCreate));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    try {
                        switch (inputLine.trim()) {
                            case "FLAG0":
                                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                                inputLine = " <li>&nbsp;Mã hóa đơn: " + hoaDonXem.getMahoadon()+ "</li>\n";
                                inputLine += " <li>Người lập:&nbsp; (" + nhanVienLapHoaDon.getMSNV() +") " + nhanVienLapHoaDon.getHoten()+"</li>\n";
                                inputLine += " <li>Ngày lập:&nbsp;" + sdf.format(hoaDonXem.getThoidiemdong()) + "</li>\n";
                                break;
                            case "FLAG1":
                                //Thông tin người thuê
                                sdf = new SimpleDateFormat("dd-MM-yyyy");
                                inputLine = " <li>&nbsp;Số CMND: " + nguoiThue.getCMND() + "</li>\n";
                                inputLine += " <li>&nbsp;Tên chủ nhà trọ: " + nguoiThue.getHoten() + "</li>\n";
                                inputLine += " <li>Giới t&iacute;nh:&nbsp;" + nguoiThue.getGioitinh() + "</li>\n";
                                inputLine += " <li>Ng&agrave;y sinh:&nbsp;" + sdf.format(nguoiThue.getNgaysinh()) + "</li>\n";
                                inputLine += " <li>SĐT:&nbsp;" + nguoiThue.getSdt() + "</li>\n";
                                inputLine += " <li>Công việc:&nbsp;" + nguoiThue.getCongviec() + "</li>\n";
                                break;
                            case "FLAG2":
                                //Thông tin nhà trọ + phòng trọ
                                DiaChi dc = nhaTroXem.getDiachi();
                                inputLine = "<li>Tên nhà trọ:&nbsp;" + nhaTroXem.getTennhatro() + "</li>";
                                inputLine += "<li>Địa chỉ:&nbsp;" + dc.getSonha() + "/" + dc.getHem() + ", " + dc.getDuong() + ", " + dc.getQuanhuyen() + ", " + dc.getTinhtp() + "</li>";
                                inputLine += "<li>Mã phòng:&nbsp;" + phongTroXem.getMaphong() + "</li>";
                                inputLine += "<li>Số người ở:&nbsp;" + phongTroXem.getLoaiphong().getSucchua() + "</li>";
                                inputLine += "<li>Loại phòng:&nbsp;" + phongTroXem.getLoaiphong().getMotaloai() + "</li>";
                                inputLine += "<li>Ghi chú:&nbsp;" + hoaDonXem.getGhichu() + "</li>";
                                break;
                            case "FLAG3":
                                //Thông tin danh sách các tháng đóng
                                GiaPhongController giaPhongControl = new GiaPhongController(this.conn);
                                int stt = 0;
                                inputLine = "";
                                long tien = Long.parseLong(giaPhongControl.giaPhong(phongTroXem.getLoaiphong().getDschitietgia()));
                                long tongTien = tien * ((hoaDonXem.getDenthang() - hoaDonXem.getTuthang() + 1));
                                for (int i = hoaDonXem.getTuthang(); i <= hoaDonXem.getDenthang(); i++) {
                                    stt++;
                                    inputLine += "   <tr>\n"
                                            + "                    <td style=\"width:20%\">\n"
                                            + "                        <p>" + stt + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:40%\">\n"
                                            + "                        <p>" + i + "</p>\n"
                                            + "                    </td>\n"
                                            + "                    <td style=\"width:40%\">\n"
                                            + "                        <p>" + XuLyCurrency.priceWithDecimal(tien + "") + "</p>\n"
                                            + "                    </td>\n"
                                            + "                </tr>";
                                }
                                inputLine += "   <tr>\n<td style=\"width:20%\"></td>\n"
                                        + "                    <td style=\"width:40%\">\n"
                                        + "                        <p>Thành tiền:</p>\n"
                                        + "                    </td>\n"
                                        + "                    <td style=\"width:40%\">\n"
                                        + "                        <p>" + XuLyCurrency.priceWithDecimal(tongTien + "") + "</p>\n"
                                        + "                    </td>\n"
                                        + "                </tr>";
                                break;
                            case "FLAG4":
                                //Ngày in người in - nguoi lập - thoi gian lập
                                Calendar cal = Calendar.getInstance();
                                SimpleDateFormat sdfwithtime = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
                                inputLine = "<i>Thời điểm in:&nbsp;" + sdfwithtime.format(cal.getTime()) + ".<br/>Nhân viên in:&nbsp;(" + nhanVienIn.getMSNV() + ") " + nhanVienIn.getHoten() + ".</i>";
                                break;
                        }
                        writer.write(inputLine + "\n");
                    } catch (IOException e) {
                    }
                }
            }

            writer.close();
        } catch (IOException e) {
        }
        String urlFilePDF = this.getCurrentPath() + "src/files/" + hoaDonXem.getMahoadon() + ".pdf";
        if (this.convertToPDF(urlFileCreate, urlFilePDF)) {
            try {
                File file = new File(urlFileCreate);
                if (file.delete()) {
                    System.out.println(file.getName() + " is deleted!");
                } else {
                    System.out.println("Delete operation is failed.");
                }
                return urlFilePDF;

            } catch (Exception e) {
            }
        } else {
            try {
                File file = new File(urlFileCreate);
                if (file.delete()) {
                    System.out.println(file.getName() + " is deleted!");
                } else {
                    System.out.println("Delete operation is failed.");
                }
            } catch (Exception e) {
            }
        }
        return null;
    }

    public void callOpen(String urlFile) {
        if (Desktop.isDesktopSupported()) {
            try {
                File myFile = new File(urlFile);
                Desktop.getDesktop().open(myFile);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Tệp tin không tồn tại. Vui lòng kiểm tra lại!", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private boolean convertToPDF(String urlHTMl, String urlPDF) {
        try {
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(urlPDF));
            document.open();
            XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(urlHTMl));
            document.close();
            return true;
        } catch (DocumentException | IOException ex) {
            return false;
        }
    }

}
